<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 定时任务
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.jobName" placeholder="任务名" class="filter-item handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    v-loading="loading"
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="jobName" label="任务名"></el-table-column>
                <el-table-column prop="className" label="类"></el-table-column>
                <el-table-column prop="methodName" label="方法"></el-table-column>
                <el-table-column prop="cronExpression" label="执行表达式"></el-table-column>
                <el-table-column label="状态" align="center" >
                    <template slot-scope="scope">
                        <el-tag type="success">
                            {{selectDictLabel(form.statusOptions, scope.row.status)}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseDateTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="220" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-user-solid"
                                @click="handleResumeJob(scope.$index, scope.row)"
                        >启动</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handlePauseJob(scope.$index, scope.row)"
                        >暂停</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleRunJob(scope.$index, scope.row)"
                        >执行一次</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.current"
                    :page-size="query.size"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" :rules="rules" label-width="70px">
                <el-form-item label="任务名" prop="jobName" >
                    <el-input v-model="form.jobName"></el-input>
                </el-form-item>
                <el-form-item label="表达式" prop="cronExpression">
                    <el-input v-model="form.cronExpression"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { sysJobPage,updateSysJob,sysJobResume,sysJobPause,sysJobRun } from '../../api/sysJob';

export default {
    name: 'login-log-list',
    data() {
        return {
            // 遮罩层
            loading: true,
            query: {
                jobName: '',
                current: 1,
                size: 10
            },
            editVisible:false,
            form:{
                cronExpression:"",
                statusOptions: [{
                    value: '0',
                    name: '正常'
                }, {
                    value: '1',
                    name: '暂停'
                }]
            },
            tableData: [],
            pageTotal: 0,
            rules: {
                jobName: [
                    { required: true, message: '请输入名称', trigger: 'blur' }
                ]
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.loading = true;
            sysJobPage(this.query).then(res => {
                this.loading = false;
                this.tableData = res.data;
                this.pageTotal = res.count;
            });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {

                })
                .catch(() => {});
        },
        handleResumeJob(index, row) {
            this.$confirm('确定要启动吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    sysJobResume(row.id).then(res => {
                        if (res.code == 0) {
                            this.$message.success(`启动成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                })
                .catch(() => {});
        },
        handlePauseJob(index, row) {
            this.$confirm('确定要暂停吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    sysJobPause(row.id).then(res => {
                        if (res.code == 0) {
                            this.$message.success(`暂停成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                })
                .catch(() => {});
        },
        handleRunJob(index, row) {
            this.$confirm('确定要执行一次吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    sysJobRun(row.id).then(res => {
                        if (res.code == 0) {
                            this.$message.success(`执行成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                })
                .catch(() => {});
        },
        handleEdit(index, row) {
            this.form.id = row.id;
            this.form.jobName = row.jobName;
            this.form.cronExpression = row.cronExpression;
            this.editVisible = true;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        // 保存编辑
        saveEdit() {
            this.$refs['form'].validate((valid) => {
                if (!valid) {
                    console.log('error submit!!');
                    return false;
                }
                if (this.form.id) {
                    updateSysJob({id:this.form.id,cronExpression:this.form.cronExpression}).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`修改成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'current', 1);
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'current', val);
            this.getData();
        }
    },
    filters:{
        showStatus(status){
            if (status == "0") {
                return "成功";
            } else if (status == "1") {
                return "失败";
            } else {
                return "";
            }
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
